<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/10/19
 * Time: 11:58
 */

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;

class GroupBuyGoods extends BaseDbModel
{
	const _CONFIG_FILE_ = 'db_shop';

	const _CONFIG_SELECT_ = 'db_activity';

	const _DATABASE_ = 'shop_activity';

	const _TABLE_NAME = 'group_buy_goods';


	const STATUS_NORMAL = 1;
	const STATUS_DELECT = 9;

	public static function saveData($data, $groupBuyId)
	{
		foreach ($data as $item) {
			$saveData = [
				'group_buy_id' => $groupBuyId,
				'shop_id' => $item['shop_id'],
				'goods_id' => $item['goods_id'],
				'sku_id' => $item['sku_id'],
				'group_buy_price' => $item['group_buy_price'],
				'start_time' => $item['start_time'],
				'end_time' => $item['end_time'],
			];
			$where =  ['group_buy_id' => $groupBuyId, 'goods_id'=> $saveData['goods_id'], 'sku_id' => $saveData['sku_id']];
			$currentTime = date("Y-m-d H:i:s");
			if (self::get('*',$where)) {
				$results = self::update(array_merge($saveData, ['status' => self::STATUS_NORMAL, 'edittime' => $currentTime]), $where);
			} else {
				$results = self::insert(array_merge($saveData,['addtime' => $currentTime]));
			}
			if (!$results -> rowCount()) {
				throw new \Exception('更新信息失败');
			}
		}
	}

	public static function selectByGroupBuyIds($groupIds)
	{
		$infos = self::select('*', [
			'group_buy_id' => $groupIds,
			'status' => self::STATUS_NORMAL
		]);
		$rst = [];
		foreach ($infos as $info) {
			$rst[$info['group_buy_id']][$info['sku_id']] = $info;
		}

		return $rst;

	}

	public static function getNotOverInfos($goodsid)
	{
		return self::select('*', [
			'goods_id' => $goodsid,
			'end_time[>]' => date('Y-m-d H:i:s'),
			'status' => self::STATUS_NORMAL
		]);
	}
}
